let moveStart = null;
let timer = null,
  lazyTimerTime = 50;
let distance = 0;
/**
 * @description 抠图开始触发
 * @param {*} e
 */
export function touchStartMove(e) {
  if (e.touches.length == 1) {
    // 移动
    moveStart = e.touches[0];
  } else {
    // 缩放
    let { x, y } = e.touches[0];
    let startDistance = calcDistance(e.touches[0], e.touches[1]);
    distance = startDistance;
  }
}
/**
 * 计算两个手指距离
 */
export function calcDistance(pos0, pos1) {
  let xMove = pos1.x - pos0.x;
  let yMove = pos1.y - pos0.y;
  return Math.sqrt(xMove * xMove + yMove * yMove);
}

export function changePosition(eventObj) {
  clearTimeout(timer);

  let { x, y } = eventObj;
  let movePositionX = x - moveStart.x;
  let movePositionY = y - moveStart.y;
  if (x > moveStart.x) {
    // console.log('向右移动',)
  } else {
    // console.log('向左移动', movePositionX)
  }
  if (y > moveStart.y) {
    // console.log('向下移动', movePositionX)
  } else {
    // console.log('向上移动',)
  }
  moveStart = {
    x: x,
    y: y,
  };
  return {
    x: movePositionX,
    y: movePositionY,
  };
}
/**
 * @description 用户移动图片和拖动图片
 * @param {*} e
 */
export function tempMove({ e, scale }) {
  console.log("tempmove");

  let thisDistance = calcDistance(e.touches[0], e.touches[1]);
  // 计算移动的过程中实际移动了多少的距离
  let distanceDiff = thisDistance - distance;

  let newScale = scale + 0.005 * distanceDiff;
  console.log("缩放计算", {
    canvasImageScale: scale,
    distanceDiff: 0.005 * distanceDiff,
    newScale,
  });
  let multiple = 2;
  if (newScale >= multiple && multiple > 2) {
    // 原图比较大情况
    newScale = multiple;
  } else if (multiple < 2 && newScale >= 2) {
    // 原图较小情况
    newScale = 2; // 最大2倍
  }
  // 最小缩放到1
  if (newScale <= 0.2) {
    newScale = 0.2;
  }
  newScale = newScale / scale;
  console.log("timer里代码缩放比例", newScale);
  return {
    scale: newScale,
  };
}
